package com.zzj.ecology.framework.configuration;

import com.zzj.ecology.framework.interceptor.AuthorizationInterceptor;
import com.zzj.ecology.framework.interceptor.PaginationInterceptor;
import com.zzj.ecology.framework.interceptor.impl.SameUrlSubmitInterceptor;
import com.zzj.ecology.framework.resolver.PaginationHandlerMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * @ClassName CustomizeWebMvcConfigurer
 * @Description TODO
 * @Author zhijun zeng at 2020/2/29
 * @Version 1.0
 **/
@Configuration
public class CustomizeWebMvcConfigurer extends WebMvcConfigurerAdapter {

    @Autowired
    private SameUrlSubmitInterceptor sameUrlSubmitInterceptor;

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    @Autowired
    private PaginationInterceptor paginationInterceptor;
    @Autowired
    private PaginationHandlerMethodArgumentResolver paginationHandlerMethodArgumentResolver;

    /**
     * 备置 CROS 跨域,
     * spring security 配置中的protected void configure(HttpSecurity http)需要配置 http.cors().and().csrf().disable();
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //super.addCorsMappings(registry);
        registry.addMapping("/**");
    }




    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/ajax/**").excludePathPatterns("/captcha/**");
        registry.addInterceptor(sameUrlSubmitInterceptor).addPathPatterns("/ajax/**");
        registry.addInterceptor(paginationInterceptor).addPathPatterns("/ajax/**");
    }

    /**
     * SpringBoot 的 @EnableAutoConfiguration 会启用自动配置类 WebMvcAutoConfiguration，该类配置了一些默认的静态资源映射
     * 自动映射 localhost:8080/** 为以下路径
     * classpath:/public/
     * classpath:/META-INF/resources/
     * 自动映射 localhost:8080/webjars/** 为以下路径
     * classpath:/META-INF/resources/webjars/
     *
     * 如果使用addResourceHandlers(ResourceHandlerRegistry registry)上述的自动配置源就会取消
     * 如果使用了 @EnableWebMvc，则自动配置类 WebMvcAutoConfiguration 会失效，因此默认映射路径 /static, /public, META-INF/resources, /resources 都将失效
     * 配置静态访问 前缀 spring.mvc.static-path-pattern: /pages/**
     *
     *
     * 默认在src/main/resources/static的文件是可以直接访问
     * src/main/resources/static/images/1.jpg可以直接通过localhost:808/images/1.jpg直接访问
     *
     * 通过以下方法对静态文件的映射后,访问文件路径就变成如下(在WebSecurityInitialize中对静态资源的访问要放行)
     * http://localhost:808/resources/images/1.jpg
     * */
//	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
//		registry
//                .addResourceHandler("/**") //配置成 /static/**, URL地址就要以static开始, 定义访问静态页面使用static开头
//                .addResourceLocations("classpath:/static/");// spring.mvc.static-path-pattern: /static/**


        /** swagger配置 */
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
//        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(paginationHandlerMethodArgumentResolver);
    }
}
